package cn.xxstudy.visitshop.ui.activity.main.fragment.home.viewmodel

import android.view.View
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import cn.xxstudy.visitshop.network.data.InfoData
import cn.xxstudy.visitshop.ui.activity.main.fragment.home.model.BannerData
import cn.xxstudy.visitshop.ui.activity.task.model.TaskData
import kotlinx.coroutines.async
import kotlinx.coroutines.launch

/**
 * @author ：DELL on 2021/3/24 .
 * @packages ：cn.xxstudy.visitshop.ui.activity.main.fragment.home.viewmodel .
 * TODO:主页ViewModel
 */
enum class RefreshState {
    START,
    REFRESHING,
    COMPLETE
}

class HomeViewModel(private val homeRepository: HomeRepository) : ViewModel() {
    val bannerLiveData = MutableLiveData<ArrayList<BannerData>>()
    val taskLiveData = MutableLiveData<ArrayList<TaskData>>()
    val infoLiveData = MutableLiveData<ArrayList<InfoData>>()
    val failLiveData = MutableLiveData<String>()
    val refreshState = MutableLiveData<RefreshState>()

    fun refresh() {
        viewModelScope.async { homeRepository.banner(bannerLiveData, failLiveData) }
        viewModelScope.async { homeRepository.taskList(taskLiveData, failLiveData) }
        viewModelScope.async { homeRepository.infoList(infoLiveData, failLiveData) }
        refreshState.value = RefreshState.COMPLETE
    }
}